import { constantRoutes } from '@/router'
// asyncRoutes
import { getModule } from '@/api/module'
import Layout from '@/layout'
import AppMain from '@/layout/components/AppMain'

var menuParam = []

function menusToRouter(routes, data) {
  if (data != null) {
    data.forEach((item, index) => {
      let memuName = item.guid
      try{
        memuName = JSON.parse(item.param5).menuName
      }catch(error){
        
      }
      if(memuName === undefined ||memuName === ''){
        memuName=item.guid
      } 
      const menu = {
        path: '/' + item.guid,
        component: null,
        name: memuName,
        meta: { title: item.f_Icon, icon: 'el-icon-s-flag', menuid: item.guid, menuname: item.f_Icon, pageUrl: item.param5,roleguid: item.roleguid}
      }
      if (item.children) {
        if (item.url === '#') {
          menu.component = Layout
        } else {
          menu.component = AppMain
        }
        menu.children = []
        menusToRouter(menu.children, item.children)
      } else {
        if (item.param5.indexOf('{') >= 0) {
          const param5 = JSON.parse(item.param5)
          param5['guid'] = item.guid
          param5['menuName'] = item.f_Icon
          menuParam.push(param5)
        }
        menu.component = resolve => require([`@/views/${item.url}`], resolve)
      }
      routes.push(menu)
    })
  }
  return routes
}

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: [],
  menuParam: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  },
  SET_MENU_PARAM: (state, menuParam) => {
    state.menuParam = menuParam
  }
}

const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      // const accessedRoutes = []
      // if (roles.includes('admin')) {
      //   accessedRoutes = asyncRoutes || []
      // } else {
      //   accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
      // }
      // 获取菜单部分
      const routes = []
      getModule().then((response) => {
        menuParam = []
        menusToRouter(routes, JSON.parse(response.data.menus))
        // router.addRoutes(routes)
        // accessedRoutes = routes
        // console.info(menuParam)
        //console.info(menuParam)
        commit('SET_ROUTES', routes)
        commit('SET_MENU_PARAM', menuParam)
        resolve(routes)
      })
      // accessedRoutes = asyncRoutes
      // commit('SET_ROUTES', accessedRoutes)
      // resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
